const app = getApp()
import BaseModel from '../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    video_address:"",//视频地址
    videoDownload:"",//后台返回下载视频地址
    videoMesData:"",//视频详情
    video_introduction:"",//介绍
    zanSelected:"",//点赞状态
  },
  /**
   * 生命周期函数--页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
  },
  onShow: function () {
    this.getVideoMesData(); //视频详情
  },
  /*点赞*/
  clickZan:function(){
    this.setData({
      zanSelected:1
    })
    this.getZanNumMesData();
  },
  //点赞数量
  getZanNumMesModel:new BaseModel({
    url:"snapVideo",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getZanNumMesData(){
    this.getZanNumMesModel
        .fetch({
          id:this.data.id
        })
        .then(res => {
          this.getVideoMesData(); //视频详情
        })
        .catch(res => {
          //失败
        })
  },
  //视频详情
  getVideoMesModel:new BaseModel({
    url:"employees/detailVideo",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getVideoMesData(){
    this.getVideoMesModel
        .fetch({ id:this.data.id })
        .then(res => {
          //成功
          this.setData({
            videoMesData: res.data,
            video_introduction:res.data.video_introduction,
            video_address:res.data.video_address
          })
        })
        .catch(res => {
          //失败
        })
  },
  /*一键复制*/
  copy:function(){
    wx.setClipboardData({
      data: this.data.video_introduction,
      success(res) {
        wx.getClipboardData({
          success(res) {
            console.log(res.data) // data
          }
        })
      }
    })
  },
  /*下载视频*/
  getVideoDownloadModel:new BaseModel({
    url:"Pub/localfile",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getVideoDownloadData(){
    this.getVideoDownloadModel
        .fetch({ file:this.data.video_address })
        .then(res => {
          //成功
          this.setData({
            videoDownload: res.data,
          })
        })
        .catch(res => {
          //失败
        })
  },
  download:function(e){
    var that = this;
    that.getVideoDownloadData();
    wx.getSetting({
      success(res) {
        /*已经授权*/
        console.log(res.authSetting['scope.writePhotosAlbum'])
        if(res.authSetting['scope.writePhotosAlbum']==true){
          that.saveVideo();
          console.log("已经授权")
        }else{
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              // 用户已经同意小程序使用保存到相册功能，后续调用 wx.startRecord 接口不会弹窗询问
              that.saveVideo()
            },
            fail(){
              console.log("11111111111")
              wx.showModal({
                title: '温馨提示',
                content: '你关闭了访问相册的权限，无法保存，请允许访问相册',
                success(res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                    wx.openSetting({
                      success(settingdata) {
                        console.log(settingdata)
                        if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                          console.log("获取权限成功，再次点击图片保存到相册")
                          that.saveVideo()
                        } else {
                          console.log("获取权限失败")
                        }
                      }
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  saveVideo:function(){
    wx.downloadFile({
      url: this.data.videoDownload,
      success(res) {
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          console.log("200")
          wx.saveVideoToPhotosAlbum({
            filePath: res.tempFilePath,
            success(res) {
              console.log("保存成功")
            },
            error(res){
              wx.showToast({
                title: '文件过大，下载失败',
                icon: 'none',
                duration: 2000
              })
            },
            complete(res){
              console.log("保存完成")
            }
          })
        }
      },
      fail(res){
        wx.showToast({
          title: '文件过大，下载失败',
          icon: 'none',
          duration: 2000
        })
      },
      complete(res){
        console.log("保存完成")
      }
    })

  },
  //视频播放量
  getVideoNumMesModel:new BaseModel({
    url:"addOneVideo",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getVideoNumMesData(){
    this.getVideoNumMesModel
        .fetch({
          id:this.data.id
        })
        .then(res => {
          //成功
        })
        .catch(res => {
          //失败
        })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.getVideoNumMesData();//视频播放量
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getVideoMesData();
    wx.stopPullDownRefresh(); //停止下拉
  },
})